package cn.hwadee.muying.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import cn.hwadee.muying.commons.base.BaseController;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.hwadee.muying.service.BuildingService;
import cn.hwadee.muying.entity.Building;
import cn.hwadee.muying.commons.jqgrid.GridResult;
import cn.hwadee.muying.commons.page.MyPage;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.ui.Model;

import java.util.Map;

/**
* <p>
* 设备 前端控制器
*
* </p>
*
* @author Mankind
* @since 2018-07-18
*/
@Controller
@RequestMapping("/sys/building")
public class BuildingController extends BaseController {
    @Autowired
    private BuildingService buildingService;

    @ModelAttribute("building")
    public Building get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return buildingService.selectById(id);
        }else{
            return new Building();
        }
    }

    @RequiresPermissions("sys:building:list")
    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/biz/building/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:building:list")
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list(String keyword) throws Exception{
        MyPage<Building> page = getPage();
        page = buildingService.list(page,getCurrentUser(),keyword);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑设备界面
     * @param building
     * @param model
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:building:edit")
    @RequestMapping(value = "/edit", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute Building building,Model model) throws Exception{
        model.addAttribute("building",building);
        return "/biz/building/edit";
    }

    /**
     * 处理更新设备的请求
     * @param building
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:building:edit")
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Building building){
        Map<String,Object> result = new HashedMap();
        try {
        if(building.getId() != null && building.getId() > 0){
            buildingService.updateById(building);
        }else {
            buildingService.insert(building);
        }
        result.put("status",200);
        result.put("msg","设备信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        result.put("status",500);
        result.put("msg","设备信息保存失败");
        }
        return result;
    }

    /**
     * 处理删除设备的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:building:delete")
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashedMap();
        try {
        if(id != null && id > 0){
            buildingService.deleteById(id);
            result.put("status",200);
            result.put("msg","删除设备成功");
        }else {
            result.put("status",500);
            result.put("msg","请选择删除的设备");
        }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除设备失败");
        }
        return result;
    }

}
